package org.longteng.security.jwtkeys;


import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * rsa key实体类
 */
public class JwtRSAKey extends JwtKeyPair {
    static final String KEY_ALGORITHM = "RSA";

    /**
     * 构建后要调用 initBuildKey
     * @param keyId
     * @param keySize
     */
    public JwtRSAKey(String keyId, int keySize) {
        super();
        this.keyId = keyId;
        this.keySize = keySize;

    }
    public JwtRSAKey(RSAPublicKey publicKey, RSAPrivateKey privateKey){
        super();
        this.publicKey = publicKey;
        this.privateKey = privateKey;
        this.keyId = UUID.randomUUID().toString();
    }

    @Override
    public RSAPrivateKey getPrivateKey() {
        return (RSAPrivateKey)privateKey;
    }

    public void setPrivateKey(RSAPrivateKey privateKey) {
        this.privateKey =  privateKey;
    }

    @Override
    public RSAPublicKey getPublicKey() {
        return (RSAPublicKey)publicKey;
    }

    public void setPublicKey(RSAPublicKey publicKey) {
        this.publicKey =  publicKey;
    }

    /**
     * 返回实现的加密类型
     *
     * @return
     */
    @Override
    public String getAlgorithmName() {
        return KEY_ALGORITHM;
    }

    /**
     * 返回实现的加签名类型
     *
     * @return
     */
    @Override
    public String getAlgSignatureName() {
        return KEY_ALGORITHM;
    }

    /**
     * 生成RSA的密钥对
     * @return
     */
    @Override
    public JwtKeyPair initBuildKey() throws NoSuchAlgorithmException {
        /* RSA算法要求有一个可信任的随机数源 */
        //获得对象 KeyPairGenerator 参数 RSA 1024个字节
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
        keyPairGen.initialize(this.keySize);  // 1024,3072

        //通过对象 KeyPairGenerator 生成密匙对 KeyPair
        KeyPair keyPair = keyPairGen.generateKeyPair();

        //通过对象 KeyPair 获取RSA公私钥对象RSAPublicKey RSAPrivateKey
        this.publicKey = (RSAPublicKey) keyPair.getPublic();
        this.privateKey = (RSAPrivateKey) keyPair.getPrivate();

        return this;
    }

    /**
     * 公钥字符串转换成公钥
     *
     * @param pubKeyBaseStr
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     */
    @Override
    public PublicKey reaPubKey(String pubKeyBaseStr) throws NoSuchAlgorithmException, InvalidKeySpecException {
        // 创建 已编码的公钥规格
        KeySpec encPubKeySpec = readPubKeySpec(pubKeyBaseStr);

        // 获取指定算法的密钥工厂, 根据 已编码的公钥规格, 生成公钥对象
        PublicKey pubKey = KeyFactory.getInstance(KEY_ALGORITHM).generatePublic(encPubKeySpec);
        return pubKey;
    }

    /**
     * 私钥字符串转换成私钥
     *
     * @param priKeyBaseStr
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     */
    @Override
    public PrivateKey reaPriKey(String priKeyBaseStr) throws NoSuchAlgorithmException, InvalidKeySpecException {
        // 创建 已编码的私钥规格
        KeySpec encPriKeySpec = readPriKeySpec(priKeyBaseStr);

        // 获取指定算法的密钥工厂, 根据 已编码的私钥规格, 生成私钥对象
        PrivateKey priKey = KeyFactory.getInstance(KEY_ALGORITHM).generatePrivate(encPriKeySpec);
        return priKey;
    }

}
